/**在引入该js前,请确保已经引入jQuery以及layer.js
 * 该js主要用于流程的发起以及表单的处理
 */
function saveProcessForm() {
    var processData=$("#process-data").serializeJSON();
    var formData=$("#form-data").serializeJSON();
    $.post("/process/saveFormData",{"processData":JSON.stringify(processData),"formData":JSON.stringify(formData)},function (response) {
        alert(response.code);
    })


}

function saveAndStartProcess() {
    var processData=$("#process-data").serializeJSON();
    var formData=$("#form-data").serializeJSON();
   var handlerData={};
    var nextHandlerNodes =handlerTree.getCheckedNodes(true);
    if(0==nextHandlerNodes.length){
        layer.msg('请选择下一步处理人！');
    }else{
        handlerData['nextHandler']=nextHandlerNodes[0].id;
    }
    var copyto=[];
    var copytoNodes = copytoTree.getCheckedNodes(true);
    $.each(copytoNodes,function(index,item){
        if(!item.isParent){
            copyto.push(item.id);
        }
    });
    handlerData['copyto']=copyto;
    $.post("/process/start",{"processData":JSON.stringify(processData),"formData":JSON.stringify(formData),"handlerData":JSON.stringify(handlerData)},function (response) {
        alert(response.code);
    })
}

function taskProcess(taskId){
    layer.open({
        type:2,
        title:'任务办理',
        area:['60%','60%'],
        content:'/task/toHandleView/processView?taskId='+taskId
    })
}

function jumpTo(){

}
function addSign(taskId){
    layer.open({
        type:2,
        title:'加签(发起会签)',
        area:['60%','60%'],
        content:'/task/toHandleView/addSignView?taskId='+taskId
    })
}

function sendBack(){

}
function processReject(taskId){
    layer.open({
        type:2,
        title:'任务办理',
        area:['60%','60%'],
        content:'/task/toHandleView/rejectView?taskId='+taskId
    })
}

function processEnd(taskId){
    function processReject(taskId){
        layer.open({
            type:2,
            title:'任务办理',
            area:['60%','60%'],
            content:'/task/toHandleView/endProcessView?taskId='+taskId
        })
    }
}

function counterSignSubmit(taskId) {
    $.post("/task/counterSignSubmit",{"taskId":taskId,"opinion":$("#opinion").val()},function (response) {
        if(0==response.code){
            layer.msg("办理完成,两秒后页面自动关闭并刷新...");
            setTimeout(function () {
                layer.close(layer.index);
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
            },2000);

        }
    });
}


function getProcessHistoryView(){

}

function getProcessView(){
    
}

function processDataInit(){

}
var handlerTree;
var copytoTree;
function  handlerTreeInit(userList) {
    var setting = {
        check: {
            enable: true,
            chkStyle: "radio",
            radioType: "all"
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    handlerTree = $.fn.zTree.init($("#handler-tree"), setting, userList);
}
function copytoTreeInit(userList){
    var setting = {
        check: {
            enable: true,
            chkStyle: "checkbox",
            radioType: "all"
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    copytoTree = $.fn.zTree.init($("#copyto-tree"), setting, userList);
}
$(function () {
    $.get("/authUser/listUserTree", function (response) {
        var userList = response.data;
        handlerTreeInit(userList);
        copytoTreeInit(userList);
    })
})
